package com.epimarket.controller;

import java.io.FileOutputStream;
import java.util.Date;
import java.util.Set;

import com.epimarket.database.EMF;
import com.epimarket.entity.Book;
import com.epimarket.entity.Purchase;
import com.epimarket.webdata.WD;
import com.itextpdf.text.Anchor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class BillGenerator {

	private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
			Font.BOLD);
	private static Font nFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
			Font.NORMAL);
	private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
			Font.BOLD);
	private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
			Font.BOLD);

	public	static void	generate(Purchase p) {

		try {
			String file = "/home/bill" + p.getId();
			Document document = new Document();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			document.open();
			addMetaData(document, p);
			addTitlePage(document, p);
			addContent(document, p);
			document.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void addContent(Document document, Purchase p) throws DocumentException {
		Anchor anchor = new Anchor("Your command", catFont);
		anchor.setName("Your command");

		// Second parameter is the number of the chapter
		Chapter catPart = new Chapter(new Paragraph(anchor), 1);

		Paragraph subPara = new Paragraph("Detail", subFont);
		addEmptyLine(subPara, 1);
		Section subCatPart = catPart.addSection(subPara);

		// Add a table
		createTable(subCatPart, p);
		addEmptyLine(subPara, 3);

		subPara = new Paragraph("Total", subFont);
		subCatPart = catPart.addSection(subPara);
		addEmptyLine(subPara, 1);
		System.out.println("price:" + p.getFullPrice());
		subCatPart.add(new Paragraph("$ " + ((Double)(p.getFullPrice())).toString()));

		// Now add all this to the document
		document.add(catPart);
	}

	private static void createTable(Section subCatPart, Purchase p) {
		PdfPTable table = new PdfPTable(5);

		// t.setBorderColor(BaseColor.GRAY);
		// t.setPadding(4);
		// t.setSpacing(4);
		// t.setBorderWidth(1);

		PdfPCell c1 = new PdfPCell(new Phrase("Book Title"));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);

		c1 = new PdfPCell(new Phrase("Author"));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);

		c1 = new PdfPCell(new Phrase("Price"));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		
		c1 = new PdfPCell(new Phrase("Quantity"));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		
		c1 = new PdfPCell(new Phrase("Total"));
		c1.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(c1);
		table.setHeaderRows(1);

		Set<Integer> setId = WD.getData().getUser().getCart().keySet();
		
		for (Integer i : setId) {
			Book b = (Book)(EMF.getSession().get(Book.class, i));
			table.addCell(b.getTitle());
			table.addCell(b.getAuthor().getLastName() + " " + b.getAuthor().getFirstName());
			table.addCell("$ " + ((Double)(b.getPrice())).toString());
			table.addCell(WD.getData().getUser().getCart().get(i).toString());
			table.addCell("$ " + ((Double)(WD.getData().getUser().getCart().get(i) * b.getPrice())).toString());
		}

		subCatPart.add(table);
	}

	private static void addTitlePage(Document document, Purchase p) throws DocumentException {
		Paragraph preface = new Paragraph();
		// We add one empty line
		addEmptyLine(preface, 1);
		// Lets write a big header
		preface.add(new Paragraph("Bill number " + p.getId(), catFont));

		addEmptyLine(preface, 1);
		// Will create: Report generated by: _name, _date
		preface.add(new Paragraph(
				"Bill generated by: epimarket" + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				smallBold));
		addEmptyLine(preface, 3);
		preface.add(new Paragraph(
				"This document the detail of your command (number " + p.getId() + ")",
				smallBold));

		addEmptyLine(preface, 8);

		preface.add(new Paragraph("Your informations :", subFont));
		addEmptyLine(preface, 1);
		preface.add(new Paragraph("Name : " + WD.getData().getUser().getUser().getLogin(), nFont));
		preface.add(new Paragraph("Email : " + WD.getData().getUser().getUser().getMail(), nFont));
		addEmptyLine(preface, 3);

		preface.add(new Paragraph("Our informations :", subFont));
		addEmptyLine(preface, 1);
		preface.add(new Paragraph("Society : Epimarket", nFont));

		document.add(preface);
		// Start a new page
		document.newPage();
	}

	private static void addMetaData(Document document, Purchase p) {
		document.addTitle("Bill number " + p.getId());
		document.addSubject("Bill for book command");
		document.addKeywords("Java, PDF, iText, Bill");
		document.addAuthor("epimarket");
		document.addCreator("epimarket");
	}

	private static void addEmptyLine(Paragraph paragraph, int number) {
		for (int i = 0; i < number; i++) {
			paragraph.add(new Paragraph(" "));
		}
	}
}
